﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using curriculum_design.Model;

namespace curriculum_design.Data
{
    public class curriculum_designContext : DbContext
    {
        public curriculum_designContext (DbContextOptions<curriculum_designContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置TestItem的外键关系
            modelBuilder.Entity<TestItem>()
                .HasOne(ti => ti.Device) // TestItem有一个Device
                .WithMany(d => d.TestProjects) // Device有多个TestItems
                .HasForeignKey(ti => ti.DeviceId); // 外键是DeviceId

            // 配置Test和Result的外键关系
            modelBuilder.Entity<ResultClass>()
                .HasOne(r => r.Test) // Result有一个Test
                .WithOne(t => t.Result) // Test有一个Results
                .HasForeignKey<ResultClass>(r => r.TestId); // 指定外键为Result的TestId
        }

        public DbSet<curriculum_design.Model.Device> Device { get; set; } = default!;
        public DbSet<curriculum_design.Model.TestItem> TestItem { get; set; } = default!;
        public DbSet<curriculum_design.Model.ResultClass> ResultClass { get; set; } = default!;
    }
}
